package com.appspot.niehonglei.action;


import java.util.Date;

import javax.jdo.PersistenceManager;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.appspot.niehonglei.common.Tools;
import com.appspot.niehonglei.dao.PMF;
import com.appspot.niehonglei.jdo.Common;

public class SaveCommon implements ActionInterface {

	@Override
	public void execute(HttpServletRequest request, HttpServletResponse response) {

		String title = request.getParameter("title");
		String link = request.getParameter("link");
		String type = request.getParameter("type");
		String id = request.getParameter("id");
		try {
			if (!"".equals(Tools.dealNull(title))
					&& !"".equals(Tools.dealNull(link))
					&& !"".equals(Tools.dealNull(type))) {
				PersistenceManager pm = PMF.get().getPersistenceManager();
				Common common = null;
				if (!"".equals(Tools.dealNull(id))) {
					common = pm.getObjectById(Common.class, Long.valueOf(id));
					common.setChangeDate(Tools.shiftDate(new Date()));
					common.setTitle(title);
					common.setLink(link);
					common.setType(Long.valueOf(type));
				} else {
					common = new Common(title, link, Long.valueOf(type));
				}
				pm.makePersistent(common);
				pm.close();
			}
			response.sendRedirect("/common?new&type=" + type);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
